﻿<!DOCTYPE html>
<html lang="en">
<head profile="http://a9.com/-/spec/opensearch/1.1/">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="./site.css" rel="stylesheet">
<title>path</title>
</head>
<body>
<div class="container">
    <h2 id="pkg-overview">package path</h2>
    <p><code>import "path"</code>
    <p>path实现了对斜杠分隔的路径的实用操作函数。</p>
    <h3 id="pkg-index" class="section-header">Index <a class="permalink" href="#pkg-index">&para;</a></h3>
    <a href="../main.html"><h3>返回首页</h3></a>
		</br>
        <li><a href="#pkg-variables">Variables</a></li>
        <li><a href="#IsAbs">func IsAbs(path string) bool</a></li>
        <li><a href="#Split">func Split(path string) (dir, file string)</a></li>
        <li><a href="#Join">func Join(elem ...string) string</a></li>
        <li><a href="#Dir">func Dir(path string) string</a></li>
        <li><a href="#Base">func Base(path string) string</a></li>
        <li><a href="#Ext">func Ext(path string) string</a></li>
        <li><a href="#Clean">func Clean(path string) string</a></li>
        <li><a href="#Match">func Match(pattern, name string) (matched bool, err error)</a></li>
    </ul>
    <h4 id="pkg-examples">Examples <a class="permalink" href="#pkg-index">&para;</a></h4>
    <a href="../main.html"><h3>返回首页</h3></a>
		</br>
        <li><a href="#example-Base" onclick="$('#ex-Base').addClass('in').removeClass('collapse').height('auto')">Base</a></li>
        <li><a href="#example-Clean" onclick="$('#ex-Clean').addClass('in').removeClass('collapse').height('auto')">Clean</a></li>
        <li><a href="#example-Dir" onclick="$('#ex-Dir').addClass('in').removeClass('collapse').height('auto')">Dir</a></li>
        <li><a href="#example-Ext" onclick="$('#ex-Ext').addClass('in').removeClass('collapse').height('auto')">Ext</a></li>
        <li><a href="#example-IsAbs" onclick="$('#ex-IsAbs').addClass('in').removeClass('collapse').height('auto')">IsAbs</a></li>
        <li><a href="#example-Join" onclick="$('#ex-Join').addClass('in').removeClass('collapse').height('auto')">Join</a></li>
        <li><a href="#example-Split" onclick="$('#ex-Split').addClass('in').removeClass('collapse').height('auto')">Split</a></li>
    </ul>
    <h3 id="pkg-variables">Variables <a class="permalink" href="#pkg-index">&para;</a></h3>
    <pre>var <span id="ErrBadPattern">ErrBadPattern</span> = <a href="errors.htm">errors</a>.<a href="errors.htm#New">New</a>(&#34;syntax error in pattern&#34;)</pre>
    <p>ErrBadPattern表示一个glob模式匹配字符串的格式错误。</p>
    <h3 id="IsAbs">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/path/path.go?name=release#196">IsAbs</a> <a class="permalink" href="#pkg-index">&para;</a></h3>
    <pre class="funcdecl">func IsAbs(path <a href="builtin.htm#string">string</a>) <a href="builtin.htm#bool">bool</a></pre>
    <p>IsAbs返回路径是否是一个绝对路径。</p>
    <div class="panel-group">
        <div class="panel panel-default" id="example-IsAbs">
            <div class="panel-heading" onclick="document.getElementById('ex-IsAbs').style.display = document.getElementById('ex-IsAbs').style.display=='none'?'block':'none';">Example</div>
            <div id="ex-IsAbs" class="panel-collapse collapse">
                <div class="panel-body">
                    <pre>fmt.Println(path.IsAbs(&#34;/dev/null&#34;))</pre>
                    <p>Output:
                    <pre>true
</pre>
                </div>
            </div>
        </div>
    </div>
    <h3 id="Split">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/path/path.go?name=release#142">Split</a> <a class="permalink" href="#pkg-index">&para;</a></h3>
    <pre class="funcdecl">func Split(path <a href="builtin.htm#string">string</a>) (dir, file <a href="builtin.htm#string">string</a>)</pre>
    <p>Split函数将路径从最后一个斜杠后面位置分隔为两个部分（dir和file）并返回。如果路径中没有斜杠，函数返回值dir会设为空字符串，file会设为path。两个返回值满足path == dir+file。</p>
    <div class="panel-group">
        <div class="panel panel-default" id="example-Split">
            <div class="panel-heading" onclick="document.getElementById('ex-Split').style.display = document.getElementById('ex-Split').style.display=='none'?'block':'none';">Example</div>
            <div id="ex-Split" class="panel-collapse collapse">
                <div class="panel-body">
                    <pre>fmt.Println(path.Split(&#34;static/myfile.css&#34;))</pre>
                    <p>Output:
                    <pre>static/ myfile.css
</pre>
                </div>
            </div>
        </div>
    </div>
    <h3 id="Join">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/path/path.go?name=release#150">Join</a> <a class="permalink" href="#pkg-index">&para;</a></h3>
    <pre class="funcdecl">func Join(elem ...<a href="builtin.htm#string">string</a>) <a href="builtin.htm#string">string</a></pre>
    <p>Join函数可以将任意数量的路径元素放入一个单一路径里，会根据需要添加斜杠。结果是经过简化的，所有的空字符串元素会被忽略。</p>
    <div class="panel-group">
        <div class="panel panel-default" id="example-Join">
            <div class="panel-heading" onclick="document.getElementById('ex-Join').style.display = document.getElementById('ex-Join').style.display=='none'?'block':'none';">Example</div>
            <div id="ex-Join" class="panel-collapse collapse">
                <div class="panel-body">
                    <pre>fmt.Println(path.Join(&#34;a&#34;, &#34;b&#34;, &#34;c&#34;))</pre>
                    <p>Output:
                    <pre>a/b/c
</pre>
                </div>
            </div>
        </div>
    </div>
    <h3 id="Dir">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/path/path.go?name=release#207">Dir</a> <a class="permalink" href="#pkg-index">&para;</a></h3>
    <pre class="funcdecl">func Dir(path <a href="builtin.htm#string">string</a>) <a href="builtin.htm#string">string</a></pre>
    <p>Dir返回路径除去最后一个路径元素的部分，即该路径最后一个元素所在的目录。在使用Split去掉最后一个元素后，会简化路径并去掉末尾的斜杠。如果路径是空字符串，会返回"."；如果路径由1到多个斜杠后跟0到多个非斜杠字符组成，会返回"/"；其他任何情况下都不会返回以斜杠结尾的路径。</p>
    <div class="panel-group">
        <div class="panel panel-default" id="example-Dir">
            <div class="panel-heading" onclick="document.getElementById('ex-Dir').style.display = document.getElementById('ex-Dir').style.display=='none'?'block':'none';">Example</div>
            <div id="ex-Dir" class="panel-collapse collapse">
                <div class="panel-body">
                    <pre>fmt.Println(path.Dir(&#34;/a/b/c&#34;))</pre>
                    <p>Output:
                    <pre>/a/b
</pre>
                </div>
            </div>
        </div>
    </div>
    <h3 id="Base">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/path/path.go?name=release#176">Base</a> <a class="permalink" href="#pkg-index">&para;</a></h3>
    <pre class="funcdecl">func Base(path <a href="builtin.htm#string">string</a>) <a href="builtin.htm#string">string</a></pre>
    <p>Base函数返回路径的最后一个元素。在提取元素前会求掉末尾的斜杠。如果路径是""，会返回"."；如果路径是只有一个斜杆构成，会返回"/"。</p>
    <div class="panel-group">
        <div class="panel panel-default" id="example-Base">
            <div class="panel-heading" onclick="document.getElementById('ex-Base').style.display = document.getElementById('ex-Base').style.display=='none'?'block':'none';">Example</div>
            <div id="ex-Base" class="panel-collapse collapse">
                <div class="panel-body">
                    <pre>fmt.Println(path.Base(&#34;/a/b&#34;))</pre>
                    <p>Output:
                    <pre>b
</pre>
                </div>
            </div>
        </div>
    </div>
    <h3 id="Ext">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/path/path.go?name=release#163">Ext</a> <a class="permalink" href="#pkg-index">&para;</a></h3>
    <pre class="funcdecl">func Ext(path <a href="builtin.htm#string">string</a>) <a href="builtin.htm#string">string</a></pre>
    <p>Ext函数返回path文件扩展名。返回值是路径最后一个斜杠分隔出的路径元素的最后一个'.'起始的后缀（包括'.'）。如果该元素没有'.'会返回空字符串。</p>
    <div class="panel-group">
        <div class="panel panel-default" id="example-Ext">
            <div class="panel-heading" onclick="document.getElementById('ex-Ext').style.display = document.getElementById('ex-Ext').style.display=='none'?'block':'none';">Example</div>
            <div id="ex-Ext" class="panel-collapse collapse">
                <div class="panel-body">
                    <pre>fmt.Println(path.Ext(&#34;/a/b/c/bar.css&#34;))</pre>
                    <p>Output:
                    <pre>.css
</pre>
                </div>
            </div>
        </div>
    </div>
    <h3 id="Clean">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/path/path.go?name=release#69">Clean</a> <a class="permalink" href="#pkg-index">&para;</a></h3>
    <pre class="funcdecl">func Clean(path <a href="builtin.htm#string">string</a>) <a href="builtin.htm#string">string</a></pre>
    <p align="left">Clean函数通过单纯的词法操作返回和path代表同一地址的最短路径。</p>
    <p align="left">它会不断的依次应用如下的规则，直到不能再进行任何处理：</p>
    <pre>1. 将连续的多个斜杠替换为单个斜杠
2. 剔除每一个.路径名元素（代表当前目录）
3. 剔除每一个路径内的..路径名元素（代表父目录）和它前面的非..路径名元素
4. 剔除开始一个根路径的..路径名元素，即将路径开始处的"/.."替换为"/"</pre>
    <p align="left">只有路径代表根地址"/"时才会以斜杠结尾。如果处理的结果是空字符串，Clean会返回"."。</p>
    <p align="left">参见<a href="http://plan9.bell-labs.com/sys/doc/lexnames.html">http://plan9.bell-labs.com/sys/doc/lexnames.html</a></p>
    <div class="panel-group">
        <div class="panel panel-default" id="example-Clean">
            <div class="panel-heading" onclick="document.getElementById('ex-Clean').style.display = document.getElementById('ex-Clean').style.display=='none'?'block':'none';">Example</div>
            <div id="ex-Clean" class="panel-collapse collapse">
                <div class="panel-body">
                    <pre>paths := []string{
    &#34;a/c&#34;,
    &#34;a//c&#34;,
    &#34;a/c/.&#34;,
    &#34;a/c/b/..&#34;,
    &#34;/../a/c&#34;,
    &#34;/../a/b/../././/c&#34;,
}
for _, p := range paths {
    fmt.Printf(&#34;Clean(%q) = %q\n&#34;, p, path.Clean(p))
}</pre>
                    <p>Output:
                    <pre>Clean(&#34;a/c&#34;) = &#34;a/c&#34;
Clean(&#34;a//c&#34;) = &#34;a/c&#34;
Clean(&#34;a/c/.&#34;) = &#34;a/c&#34;
Clean(&#34;a/c/b/..&#34;) = &#34;a/c&#34;
Clean(&#34;/../a/c&#34;) = &#34;/a/c&#34;
Clean(&#34;/../a/b/../././/c&#34;) = &#34;/a/c&#34;
</pre>
                </div>
            </div>
        </div>
    </div>
    <h3 id="Match">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/path/match.go?name=release#38">Match</a> <a class="permalink" href="#pkg-index">&para;</a></h3>
    <pre class="funcdecl">func Match(pattern, name <a href="builtin.htm#string">string</a>) (matched <a href="builtin.htm#bool">bool</a>, err <a href="builtin.htm#error">error</a>)</pre>
    <p>如果name匹配shell文件名模式匹配字符串，Match函数返回真。该模式匹配字符串语法为：</p>
    <pre>pattern:
	{ term }
term:
	&#39;*&#39;                                  匹配0或多个非/的字符
	&#39;?&#39;                                  匹配1个非/的字符
	&#39;[&#39; [ &#39;^&#39; ] { character-range } &#39;]&#39;  字符组（必须非空）
	c                                    匹配字符c（c != '*', '?', '\\', '['）
	&#39;\\&#39; c                               匹配字符c
character-range:
	c           匹配字符c（c != '\\', '-', ']'）
	&#39;\\&#39; c      匹配字符c
	lo &#39;-&#39; hi   匹配区间[lo, hi]内的字符</pre>
    <p>Match要求匹配整个name字符串，而不是它的一部分。只有pattern语法错误时，会返回ErrBadPattern。</p>
</div>
</body>
</html>
